/**
 * 文件下载
 */
const filesURL = window.service.filesURL

const extractFilename = (path) => {
  const pathArray = path.split('/')
  const lastIndex = pathArray.length - 1
  return pathArray[lastIndex]
}

export function dowmFiles (path) {
  const title = path && extractFilename(path)
  const a = document.createElement('a')
  a.setAttribute('href', `${filesURL}${path}`)
  a.setAttribute('download', title)
  a.click()
}

export function blodDownFile (blob, fileName) {
  try {
    const href = (window.URL || window.webkitURL).createObjectURL(blob) // 创建下载的链接
    if (window.navigator.msSaveBlob) {
        window.navigator.msSaveBlob(blob, fileName)
    } else {
      // 谷歌浏览器 创建a标签 添加download属性下载
      const downloadElement = document.createElement('a')
      downloadElement.href = href
      downloadElement.target = '_blank'
      downloadElement.download = fileName
      document.body.appendChild(downloadElement)
      downloadElement.click() // 点击下载
      document.body.removeChild(downloadElement) // 下载完成移除元素
      window.URL.revokeObjectURL(href) // 释放掉blob对象
    }
  } catch (e) {
      console.log('下载失败')
  }
}
